<?php
class UserMenuDAO extends BaseDAO{
	/*
	 * Load on grid view to grant for user
	 */
	function loadMenuUser($userid){
		$conn = $this->db->getConnection();
        if (!$conn){
            return false;
        }
        if(isset($userid))
			$query = "select menu.id, menu.name, user_menu.role from menu left join user_menu on(menu.id = user_menu.menuid and user_menu.userid = '$userid');";
		else 
			$query = "select menu.id, menu.name, user_menu.role from menu left join user_menu on(menu.id = user_menu.menuid and user_menu.userid = null);";
		        
        $result  = $this->db->executeQuery($query);

		if ($result) {
        	$returnres = array();
            while ($row = $this->db->getReader($result)) {
               $enquiry = array();
               $enquiry['menuid'] = $row['id'];
               $enquiry['menuname'] = $row['name'];
               $enquiry['role'] = $row['role'];
               $returnres[] = $enquiry;
            }
            $this->db->closeConnection();
            return $returnres;
        }	$this->db->closeConnection();
		return false;
	}
	/*
	 * Load top menu for user
	 */
	function loadTopMenuForUser($userid){
		$conn = $this->db->getConnection();
        if (!$conn){
            return false;
        }
		$query = "select topmenu.id, topmenu.name, topmenu.targetid, topmenu.url, topmenu.icon from topmenu where topmenu.id in(select distinct(menu.topmenuid) from menu where menu.id in(select user_menu.menuid from user_menu where user_menu.userid = $userid and user_menu.role <> 0)) and topmenu.active = 1 ORDER BY orderdisplay ASC;";
        $result  = $this->db->executeQuery($query);
		if ($result) {
        	$returnres = array();
            while ($row = $this->db->getReader($result)) {
               $enquiry = array();
               $enquiry['id'] = $row['id'];
               $enquiry['name'] = $row['name'];
               $enquiry['targetid'] = $row['targetid'];
               $enquiry['url'] = $row['url'];
               $enquiry['icon'] = $row['icon'];
               $returnres[] = $enquiry;
            }
            $this->db->closeConnection();
            return $returnres;
        }	$this->db->closeConnection();
		return false;
	}
	/*
	 * Load menu for user
	 */
	function loadMenuForUser($userid, $topmenuid){
		$conn = $this->db->getConnection();
        if (!$conn){
            return false;
        }
		$query = "select menu.id, menu.topmenuid, menu.name, menu.targetid, menu.url, user_menu.role from user_menu left join menu on(user_menu.menuid = menu.id) where user_menu.userid = $userid and user_menu.role <> 0 and menu.active = 1 and menu.topmenuid = $topmenuid ORDER BY orderdisplay ASC;";
        $result  = $this->db->executeQuery($query);
		if ($result) {
        	$returnres = array();
            while ($row = $this->db->getReader($result)) {
               $enquiry = array();
               $enquiry['id'] = $row['id'];
               $enquiry['topmenuid'] = $row['topmenuid'];
               $enquiry['name'] = $row['name'];
               $enquiry['targetid'] = $row['targetid'];
               $enquiry['url'] = $row['url'];
               $enquiry['role'] = $row['role'];
               $returnres[] = $enquiry;
            }
            $this->db->closeConnection();
            return $returnres;
        }	$this->db->closeConnection();
		return false;
	}
}
?>